package com.battle.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.battle.pojo.entity.StudyPlan;
import com.battle.pojo.enums.StudyPlanTypeEnum;
import com.battle.service.mapper.StudyPlanMapper;
import com.battle.service.repository.in.StudyPlanRepository;
import nirvana.core.repository.Repository;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
    import com.battle.pojo.dto.studyPlan.*;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
* 【描 述】：
* 【环 境】：J2SE
* 【详 细】：
*
*/
@Service
public class StudyPlanRepositoryImpl extends Repository< StudyPlanMapper , StudyPlan> implements StudyPlanRepository {

private final StudyPlanMapper mapper;

public StudyPlanRepositoryImpl(StudyPlanMapper mapper) {
this.mapper = mapper;
}

    /**
    * 搜索学习计划
    * @param dto
    * @return
    */
    @Override
    public Page< StudyPlan > selectStudyPlan(SelectStudyPlanDto dto){

    return mapper.selectStudyPlan(dto);
    }

@Override
public  Map<Long, StudyPlan> getMap(List<Long> ids){
    Map<Long, StudyPlan> map = new HashMap<>();

    if (CollectionUtil.isNotEmpty(ids)) {
        List<StudyPlan> list =listByIds(ids);
        if (CollectionUtil.isNotEmpty(list)) {
        map = list.stream().collect(Collectors.toMap(StudyPlan::getId, pojo -> pojo));
         }
        }
        return map;
    }

    /**
     * 根据条件获取
     * @param time
     * @param type
     * @return
     */
    @Override
    public List<StudyPlan> getStudyPlanList(LocalDate time, StudyPlanTypeEnum type,Long id) {
        return mapper.selectList(new LambdaQueryWrapper<StudyPlan>()
                .eq(StudyPlan::getType,type)
                .eq(StudyPlan::getIsDelete,false)
                .eq(StudyPlan::getIsDisable,false)
                .le(StudyPlan::getStartTime,time)
                .ge(StudyPlan::getEndTime,time)
                .ne(ObjectUtil.isNotEmpty(id),StudyPlan::getId,id)
        );
    }
}